package com.xgimi.open.skill.datamanager.biz.common.exception;

import com.xgimi.commons.util.StringUtil;
import com.xgimi.open.skill.datamanager.biz.common.Enum.ExceptionErrorCode;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Collection;

/**
 * @author coderzpw.zhang
 * @version 1.0
 * @description 描述
 * @date 2023/2/3 15:02
 */
public class BizAssert {
    public static void assertTrue(Boolean condition) {
        assertTrue(condition, ExceptionErrorCode.UN_KNOW_ERROR, "系统错误");
    }

    public static void assertTrue(Boolean condition, ExceptionErrorCode errorCode) {
        assertTrue(condition, errorCode, null);
    }

    public static void assertTrue(Boolean condition, ExceptionErrorCode errorCode, String msg) {
        if (condition == null || !condition) {
            throw new BizException(errorCode, msg);
        }
    }

    public static void notNull(Object object) {
        notNull(object, ExceptionErrorCode.UN_KNOW_ERROR, "系统错误");
    }

    public static void notNull(Object object, ExceptionErrorCode errorCode) {
        notNull(object, errorCode, null);
    }

    public static void notNull(Object object, ExceptionErrorCode errorCode, String msg) {
        if (null == object) {
            throw new BizException(errorCode, msg);
        }
    }

    public static void notEmpty(String text, ExceptionErrorCode errorCode) {
        if (StringUtil.isEmpty(text)) {
            throw new BizException(errorCode);
        }
    }

    public static void notEmpty(Collection collection, ExceptionErrorCode errorCode) {
        if (CollectionUtils.isEmpty(collection)) {
            throw new BizException(errorCode);
        }
    }

}
